<?php

namespace app\beautiful\controller\miniapp\member\goods;

use app\BaseController;

use app\beautiful\logic\order\OrderLogic;

use app\beautiful\model\mall\FlashSaleGoodsModel;

use app\beautiful\model\mall\FlashSaleGoodsSkuModel;

use app\beautiful\model\mall\GoodsModel;

use app\beautiful\model\logs\GoodsViewsModel;

use think\facade\Db;



class FlashSale extends BaseController{

    public function getFlashSaleGoodsList()

    {

        $today= date("y-m-d",$this->request->time());

        $where=[

            ['shop_id','=',$this->request->shopId],

            ['status','=',1],

            ['is_delete','=',0],

            ['start_time','<=',$today],

            ['end_time','>=',$today],

        ];

        //获取可用的商品id集合

        $goods_where=[

            ['shop_id','=',$this->request->shopId],

            ['status','=',1],

            ['is_delete','=',0],

            ['is_open_yu','=',0],

        ];

        $goods_ids=GoodsModel::where($goods_where)->column('goods_id') ?? [];

        $where[]=['goods_id','in',$goods_ids];



        $order=['is_tui'=>'desc','fictitious_take_count'=>'desc','flash_sale_price'=>'asc'];

        $datas=FlashSaleGoodsModel::with(['goods'=>function($query){

            $query->field('goods_id,name,original_price,cover_img');

        }])->where($where)

            ->order($order)

            ->field('flash_sale_goods_id,goods_id,flash_sale_price,fictitious_take_count,quota,end_time,take_count')

            ->limit($this->request->limit)

            ->page($this->request->page)

            ->select()

            ->append(['flash_sale_djs','flash_sale_progress'])

            ->toArray();

        $return['datas']=$datas;



        return $this->success($return);



    }



    public function getFlashSaleGoodsDetail()

    {

        $today= date("Y-m-d",$this->request->time());

        $flash_sale_goods_id=$this->request->param('flash_sale_goods_id/d',0);



        //判断秒杀

        if(null === $flash_sale_goods = FlashSaleGoodsModel::find($flash_sale_goods_id)) return $this->error('秒杀不存在');

        if($flash_sale_goods->shop_id != $this->request->shopId) return $this->error('秒杀不存在');

        if($flash_sale_goods->is_delete == 1) return $this->error('秒杀不存在');

        if($flash_sale_goods->status != 1) return $this->error('秒杀已下架');

        if($flash_sale_goods->start_time > $today) return $this->error('秒杀还未开始');

        if($flash_sale_goods->end_time < $today) return $this->error('秒杀已结束');



        //判断秒杀商品

        if(null === $goods = GoodsModel::find($flash_sale_goods->goods_id)) return $this->error('秒杀商品不存在');

        if($goods->shop_id != $this->request->shopId) return $this->error('秒杀商品不存在');

        if($goods->is_delete == 1) return $this->error('秒杀商品不存在');

        if($goods->is_open_yu == 1) return $this->error('秒杀商品不存在');

        if($goods->status != 1) return $this->error('秒杀商品已下架');



        $flash_sale_goods->append(['flash_sale_djs']);

        if($flash_sale_goods->is_have_sku==1){

            $sku=FlashSaleGoodsSkuModel::where([['flash_sale_goods_id','=',$flash_sale_goods_id],['is_delete','=',0]])

                    ->field('flash_sale_goods_sku_id,name,original_price,flash_sale_price,quota')

                    ->select()->toArray() ?? [];

        }



        $detail=[

            'flash_sale_goods_id'=>$flash_sale_goods_id,

            'goods'=>[

                'goods_id'=>$goods->goods_id,

                'rotation'=>$goods->rotation,

                'cover_img'=>$goods->cover_img,

                'name'=>$goods->name,

                'original_price'=>$goods->original_price,

                'goods_details'=>$goods->goods_details,

            ],

            'flash_sale_price'=>$flash_sale_goods->flash_sale_price,

            'quota'=>$flash_sale_goods->quota,

            'fictitious_take_count'=>$flash_sale_goods->fictitious_take_count,

            'flash_sale_djs'=>$flash_sale_goods->flash_sale_djs,

            'is_have_sku'=>$flash_sale_goods->is_have_sku,

            'sku'=>isset($sku) ? $sku :[],

            'flash_sale_info'=>$flash_sale_goods->flash_sale_info,

        ];



        $return['detail']=$detail;

        //记录秒杀对应的产品访问

        $GoodsViewsModel = new GoodsViewsModel();

        $GoodsViewsModel->save([

            'shop_id'   => $this->request->shopId,

            'goods_id'  => $goods->goods_id

        ]);

        return $this->success($return);



    }



    public function createOrder()

    {

        $logistics_type=$this->request->param('logistics_type/d');

        $receiving_name=$this->request->param('receiving_name');

        $receiving_mobile=$this->request->param('receiving_mobile');

        $receiving_address_province=$this->request->param('receiving_address_province');

        $receiving_address_city=$this->request->param('receiving_address_city');

        $receiving_address_county=$this->request->param('receiving_address_county');

        $receiving_address_info=$this->request->param('receiving_address_info');

        $total_price = $this->request->param('total_price/f',0);

        $use_integral = $this->request->param('use_integral/d',0);

        $coupon_id = $this->request->param('coupon_id/d',0);

        $goods=$this->request->param('goods/a',[]);

        $from_type = $this->request->param('from_type/d',1);

        $params=[

            'member_id'=>$this->request->memberId,

            'logistics_type'=>$logistics_type,

            'receiving_name'=>$receiving_name,

            'receiving_mobile'=>$receiving_mobile,

            'receiving_address_province'=>$receiving_address_province,

            'receiving_address_city'=>$receiving_address_city,

            'receiving_address_county'=>$receiving_address_county,

            'receiving_address_info'=>$receiving_address_info,

            'total_price'=>$total_price,

            'use_integral'=>$use_integral,

            'coupon_id'=>$coupon_id,

            'goods'=>$goods,

            'from_type'=>$from_type

        ];

        Db::startTrans();

        try{

            $order_logic = OrderLogic::init($this->request->shopId);

            $order_id=$order_logic['flash_sale']->createOrder($params);

            Db::commit();

            return $this->success(['order_id'=>$order_id]);

        } catch (\Exception $e){

            Db::rollback();

            return $this->error($e->getMessage());

        }





    }

}

